define(['app'],function(app){return {module: function($scope, $http, eventBusService, httpService, params) {
	$scope.form = {};

	//返回
	$scope.back = function(){
		let menu = {"url": "modules/content/schoolroll/student/list?father_id=" + params.father_id};
		eventBusService.publish("modules.welcome.load", menu);
	}

	//查询年级
	$scope.findGrade = function(){
		httpService.post(app.urls.grade_selectCenterGrade,{"fk_unit":$scope.form.fk_unit},function(data){
			if("0000" === data.code){
				$scope.gradeList = data.data;
			}else{
				alert(data.msg);
			}
		});
	}

	//获取班级
	$scope.findClass=function(){
		$scope.classList = [];
		httpService.post(app.urls.class_selectClassName,{"fk_unit":$scope.form.fk_unit,"fk_grade":$scope.form.fk_grade,"status":'0'},function(data){
			if("0000" === data.code){
				$scope.classList = data.data;
			}else{
				alert(data.msg);
			}
			$scope.$applyAsync();
		});
	}

	//学生状态
	$scope.findStudentStatus = function(){
		httpService.post(app.urls.dictionary_selectByRule,{fk_rule:"SJAAAAA10061"},function(data){
			if("0000" === data.code){
				$scope.statusList = data.data;
			}else{
				alert(data.msg);
			}
		});
	}

	$scope.save = function(){
		if($scope.formCheck()){
			if ($scope.form.type == '1'){
				if (typeof $scope.form.name === 'undefined' || $scope.form.name.length <= 0){
					eventBusService.publish('modal.alert', {"title":"操作提示","content":"请输入姓名。"});
					return;
				}
				if (typeof $scope.form.sfzjh === 'undefined' || $scope.form.sfzjh.length <= 0){
					eventBusService.publish('modal.alert', {"title":"操作提示","content":"请输入身份证件号。"});
					return;
				}
				if (typeof $scope.form.fk_grade === 'undefined' || $scope.form.fk_grade.length <= 0){
					eventBusService.publish('modal.alert', {"title":"操作提示","content":"请选择转入年级。"});
					return;
				}
				if (typeof $scope.form.fk_class === 'undefined' || $scope.form.fk_class.length <= 0){
					eventBusService.publish('modal.alert', {"title":"操作提示","content":"请选择转入班级。"});
					return;
				}
				if (typeof $scope.form.transfer_time === 'undefined' || $scope.form.transfer_time.length <= 0){
					eventBusService.publish('modal.alert', {"title":"操作提示","content":"请选择转入时间。"});
					return;
				}
				//登记学生信息
				httpService.post(app.urls.student_selectRepeat, $scope.form, function (data) {
					if ("0000" === data.code) {
						//生成学生信息
						httpService.post(app.urls.saveTransferInfo, $scope.form, function (data) {
							if ("0000" === data.code) {
								eventBusService.publish('modal.alert', {"title":"操作提示","content":"转入成功。"});
								eventBusService.publish("content.page.reload.",data.data);
								eventBusService.publish("modal.page.close",{});
							} else {
								alert(data.msg);
							}
						});
					} else {
						alert(data.msg);
					}
				});
			}else if ($scope.form.type == '2'){
				if (typeof $scope.form.transfer_time === 'undefined' || $scope.form.transfer_time.length <= 0){
					eventBusService.publish('modal.alert', {"title":"操作提示","content":"请选择转出时间。"});
					return;
				}
				httpService.post(app.urls.saveTransferInfo, $scope.form, function (data) {
					if ("0000" === data.code) {
						eventBusService.publish('modal.alert', {"title":"操作提示","content":"转出成功。"});
						eventBusService.publish("content.page.reload.",data.data);
						eventBusService.publish("modal.page.close",{});
					} else {
						alert(data.msg);
					}
				});
			}else{
				httpService.post(app.urls.saveTransferInfo, $scope.form, function(data){
					if("0000" === data.code){
						eventBusService.publish('modal.alert', {"title":"操作提示","content":"复学成功。"});
						eventBusService.publish("content.page.reload.",data.data);
						eventBusService.publish("modal.page.close",{});
					}else{
						alert(data.msg);
					}
				});
			}
			// $scope.back();
		}
	}

	$scope.findStudent = function (id){
		httpService.postpage(app.urls.findSchoolStudent, {'id': id},function(data){
			if("0000" === data.code){
				let student = data.data[0];
				$scope.form.fk_student = student.id;
				$scope.form.sfzjh = student.sfzjh;
				$scope.form.name = student.name;
				$scope.form.fk_grade = student.fk_grade;
				$scope.form.fk_class = student.fk_class;
				$scope.form.fk_last_state = student.state;
				$scope.findClass();
			}else{
				alert(data.msg);
			}
		});
	}

	let init = function (){
		$scope.form.fk_unit= app.userInfo.unitId;
		$scope.findGrade();
		$scope.findStudentStatus();
		setTimeout(function (){
			let item = $scope.statusList.find(item =>{
				if (params.type == '1'){
					return item.name.includes('转入');
				}else if (params.type == '2'){
					return item.name.includes('转出');
				}else {
					return item.name.includes('复学');
				}
			});
			$scope.form.state = item.id;
			if(params.type == '1' || params.type == '4'){
				let current = $scope.statusList.find(item =>{
					return item.name.includes('在读');
				});
				$scope.form.state = current.id;
				$scope.form.real_state = item.id;
			}
			$scope.$applyAsync();
		}, 1000);
		$scope.form.type = params.type;
		if (params.type == '2' || params.type == '4'){
			$scope.findStudent(params.id);
			$("#userName").attr("disabled","disabled");
			$('#idCard').attr("disabled","disabled");
		}else{
			$('#userName').removeAttr('disabled');
			$('#idCard').removeAttr('disabled');
		}
	}

	init();

}}});